package com.smkj.kaoqin.common.result;

import com.fasterxml.jackson.annotation.JsonInclude;

import com.smkj.kaoqin.common.enumeration.GlobalExceptionEnum;
import com.smkj.kaoqin.common.utils.ServletsUtils;
import lombok.experimental.Accessors;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

/**
 * @PS 返回类
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Accessors(chain = true)
public class VResponse<T> implements Serializable {

    public static final int SUCCESS = 200;

    private Integer code = SUCCESS;

    private Integer total;

    private String msg;

    private T data;


    public VResponse(int code) {
        this.code = code;
    }

    public VResponse(int errCode, String errMsg) {
        this.code = errCode;
        this.msg = errMsg;
    }

    public VResponse(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public VResponse(int code, Integer total, T data) {
        this.code = code;
        this.total = total;
        this.data = data;
    }

    public VResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public VResponse(int code, Integer total, String msg, T data) {
        this.code = code;
        this.total = total;
        this.msg = msg;
        this.data = data;
    }

    public VResponse() {

    }

    public static <T> VResponse<T> success(){
        return new VResponse<T>(SUCCESS);
    }

    public static <T> VResponse<T> success(T data){
        return new VResponse<T>(SUCCESS, data);
    }

    public static <T> VResponse<T> success(String msg){
        return new VResponse<T>(SUCCESS, msg);
    }


    public static <T> VResponse<T> success(Integer amount,T data){
        VResponse<T> response = new VResponse<T>(SUCCESS, amount, data);
        return response;
    }

    public static <T> VResponse<T> success(String msg,T data){
        VResponse<T> response = new VResponse<T>(SUCCESS, msg, data);
        return response;
    }

    public static <T> VResponse<T> success(Integer amount, String msg,T data){
        VResponse<T> response = new VResponse<T>(SUCCESS, amount, msg, data);
        return response;
    }

    public static <T> VResponse<T> error(int code,String msg){
        return new VResponse<T>(code, msg);
    }

    public static <T> VResponse<T> errorException(int code, T data){
        VResponse<T> response = new VResponse<T>(code, data);
        return response;
    }

    public static <T> VResponse<T> result(GlobalExceptionEnum globalExceptionEnum) {
        VResponse<T> response = new VResponse<T>(globalExceptionEnum.getCode(), globalExceptionEnum.getMessage());
        return response;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public VResponse setStatus(HttpStatus httpStatus) {
        HttpServletResponse response = ServletsUtils.getResponse();
        response.setStatus(httpStatus.value());
        return this;
    }

}
